package solution;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-24
 * Time: 16:40
 */

//https://leetcode.cn/problems/odd-even-linked-list/submissions/396939325/
class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

class Solution1 {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);
        ListNode l5 = new ListNode(5);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;
        oddEvenList(l1);
    }
    public static ListNode oddEvenList(ListNode head) {
        if (head == null && head.next == null) {
            return head;
        }
        ListNode list1 = head;
        ListNode dummyHead1 = new ListNode(list1.val);
        ListNode node1 = dummyHead1;
        ListNode list2 = head.next;
        ListNode dummyHead2 = new ListNode(list2.val);
        ListNode node2 = dummyHead2;
        while (list1.next != null) {
            if (list1.next.next != null) {
                node1.next = new ListNode(list1.next.next.val);
                node1 = node1.next;
            } else {
                break;
            }
            list1 = list1.next.next;
        }
        while (list2.next != null) {
            if (list2.next.next != null) {
                node2.next = new ListNode(list2.next.next.val);
                node2 = node2.next;
            } else {
                break;
            }
            list2 = list2.next.next;
        }
        node1.next = dummyHead2;
        return dummyHead1;
    }

    public ListNode oddEvenList1(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode evenHead = head.next;
        ListNode odd = head;
        ListNode even = evenHead;
        while (even != null && even.next != null) {
            odd.next = even.next;
            odd = odd.next;
            even.next = odd.next;
            even = even.next;
        }
        odd.next = evenHead;
        return head;
    }
}
